/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.tabbedproperties.section;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.LiteralString;
import org.eclipse.uml2.uml.TimeEvent;
import org.eclipse.uml2.uml.UMLFactory;

import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;

/**
 * Manages expression section of TimeEvents.
 *
 * @author David Servat
 * @since 10 apr. 07
 */
public class TimeEventExpressionSection extends PropertyViewSection{

	/**
	 * The Class DirectEditFocusListener.
	 */
	protected class DirectEditFocusListener implements org.eclipse.swt.events.FocusListener{

		/**
		 * Focus gained.
		 * 
		 * @param e the e
		 */
		public void focusGained(FocusEvent e){}

		/**
		 * Focus lost.
		 * 
		 * @param e the e
		 */
		public void focusLost(FocusEvent e){
			if(!expressionText.isDisposed()){
				if((umlElement != null) && (umlElement instanceof TimeEvent)){			
					TimeEvent timeEvent = (TimeEvent)umlElement;
					LiteralString ls = UMLFactory.eINSTANCE.createLiteralString();
					ls.setValue(expressionText.getText().trim());
					
					timeEvent.setWhen(ls);
				}
			}
		}
	}

	/**
	 * The expression text.
	 */
	private Text expressionText;

	/**
	 * The direct edit focus listener.
	 */
	private DirectEditFocusListener directEditFocusListener = new DirectEditFocusListener();

	/* (non-Javadoc)
	 * @see org.eclipse.ui.views.properties.tabbed.AbstractPropertySection#createControls(org.eclipse.swt.widgets.Composite, org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage)
	 */
	@Override
	public void createControls(Composite parent,
			TabbedPropertySheetPage tabbedPropertySheetPage) {
		super.createControls(parent, tabbedPropertySheetPage);

		final Composite composite = this.getWidgetFactory().createFlatFormComposite(parent);
		FormData data;

		expressionText = this.getWidgetFactory().createText(composite, "");
		expressionText.setEditable(true);
		// nameText.addFocusListener(directEditFocusListener);
		data = new FormData();
		data.width = 150;
		data.left = new FormAttachment(0, STANDARD_LABEL_WIDTH);
		data.right = new FormAttachment(100, 0);
		data.top = new FormAttachment(0, 0);
		expressionText.setLayoutData(data);

		expressionText.addFocusListener(directEditFocusListener);

		CLabel nameLabel = this.getWidgetFactory().createCLabel(composite, "Expr.:");
		nameLabel.setToolTipText("Expression");
		data = new FormData();
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(expressionText, -ITabbedPropertyConstants.HSPACE);
		data.top = new FormAttachment(expressionText, 0, SWT.CENTER);
		nameLabel.setLayoutData(data);
	}

	/*
	 * @see org.eclipse.ui.views.properties.tabbed.view.ITabbedPropertySection#refresh()
	 */
	@Override
	public void refresh() {
		if (!expressionText.isDisposed()){
			if(umlElement instanceof TimeEvent){
				TimeEvent timeEvent = (TimeEvent)umlElement;
				expressionText.removeFocusListener(directEditFocusListener);
				if(timeEvent.getWhen() != null){
					if(timeEvent.getWhen() instanceof LiteralString){
						LiteralString ls = (LiteralString)timeEvent.getWhen();
						String expression = ls.stringValue();
						expressionText.setText(expression.trim());
					}
					else{
						expressionText.setText("");
					}
				}
				else{
					expressionText.setText("");
				}
				expressionText.addFocusListener(directEditFocusListener);
			}				
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#dispose()
	 */
	@Override
	public void dispose(){
		super.dispose();
		if(expressionText!=null && !expressionText.isDisposed())
			expressionText.removeFocusListener(directEditFocusListener);
	}
}